import { getCategoryListByType } from '@/api/ClassManage';
import IconFont from '@/components/IconFont';
import { useAppSelector } from '@/redux/store';
import { useRequest } from 'ahooks';
import { utils, YmTree } from 'ym-design';

type NodeType = { id: string; parentId: string; title: string; key: string; num?: number };
interface LeftClassTreeProps {
  value?: string;
  onSelect?(value?: string, parentId?: string): void;
  type?: '1' | '4';
  shelves?: boolean;
  refreshDeps?: React.DependencyList;
}
export function LeftClassTree({ value, onSelect, type, refreshDeps, shelves }: LeftClassTreeProps) {
  const { entId } = useAppSelector((state) => state.loginEnt);
  // 还需要计算总数
  const { loading, data } = useRequest(
    async () => {
      const {
        data: { list },
      } = await getCategoryListByType(
        type,
        shelves,
        process.env.processEnvDetail.REACT_APP_PLATFORM === 'boss' ? undefined : entId,
      );
      const originArr: NodeType[] = [];
      let total = 0;

      list.forEach((v) => {
        const s = {
          id: v.guid,
          parentId: '',
          title: v.className,
          key: v.guid,
          num: 0,
          aiLearnClassification: v.aiLearnClassification,
        };
        originArr.push(s);
        if (v.lowerListVo.length) {
          v.lowerListVo.forEach((twolevel) => {
            originArr.push({
              id: twolevel.guid,
              parentId: v.guid,
              title: twolevel.className,
              key: twolevel.guid,
              num: twolevel.num,
            });
            s.num += twolevel.num;
          });
        }
        total += s.num;
      });

      return {
        treeData: utils.flat2Tree(originArr, ''),
        total: `全部(${total})`,
      };
    },
    {
      refreshDeps,
    },
  );

  return (
    <>
      <YmTree
        selectedKeys={value ? [value] : []}
        onSelect={(nextKeys, info) => {
          console.log(info.node, info.selectedNodes);
          if (info.node.parentId !== '' && nextKeys.length) {
            onSelect?.(nextKeys[0] as string, info.node.parentId);
          }
        }}
        defaultExpandedKeys={value ? [value] : []}
        onAllSelect={onSelect}
        treeData={data?.treeData ?? []}
        loading={loading}
        titleRender={(node: any) => {
          return (
            <div className="flexr alc jub">
              <div style={{ display: 'flex' }}>
                <span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{node.title}</span>
                <span style={{ flexShrink: 0 }}>({node.num})</span>
              </div>
              {process.env.processEnvDetail.REACT_APP_PLATFORM === 'boss' && node.aiLearnClassification == 1 ? (
                <IconFont type="icon-AI" style={{ color: '#FF6D3B', fontSize: 24 }} />
              ) : null}
            </div>
          );
        }}
        AllSelectNodeShow
        AllSelectText={data?.total ?? '全部'}
      ></YmTree>
    </>
  );
}
